/**
 * 
 */
package gui;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import util.reflection.MethodAction;

import controller.AbstractController;
import controller.SLogoController;

/**
 * The toolbar for the SLogo system
 * - uses reflections to create toolbar items as well as the actions associated with each item
 * 
 * @author Troy Ferrell
 */
public class Toolbar extends JPanel 
{
	private static final String SCANNER_DELIMITTER = "\\s+";
	
	private SLogoGUI myGUI;
	private SLogoController myController;
	
	public Toolbar(String resourceFile, SLogoGUI gui, SLogoController controller)
	{
		myGUI = gui;
		myController = controller;
		initToolbar(resourceFile);
	}
	
	/**
	 *  Create toolbar items using resourcefile
	 * - toolbar button actions point to methods in controller classes using MethodAction class
	 * 	 and reflection, resourceFile also has image location info 
	 * 
	 * @param resourceFile - file that has toolbar information
	 */
	private void initToolbar(String resourceFile)
	{
		try
		{
			Scanner reader = new Scanner(new File(resourceFile)).useDelimiter(SCANNER_DELIMITTER);
			
			while(reader.hasNext())
			{
				String buttonImg = reader.next();
				String actionMethod = reader.next();
				String controllerClassName = reader.next();
				String targetComponentName = reader.next();
				
				AbstractController controller = myController.getControllerFor(controllerClassName);
				JComponent target = myGUI.getSLogoComponent(targetComponentName);
				
				JButton newToolbarButton = new JButton(new ImageIcon(buttonImg));
				newToolbarButton.setFocusable(false);
				newToolbarButton.addActionListener( new MethodAction(Class.forName(controllerClassName).cast(controller), 
						actionMethod, Class.forName(targetComponentName).cast( target )));
				
				add(newToolbarButton);
			}
		}catch(FileNotFoundException e)
		{
			// TODO: notify prompt
		}
		catch(ClassNotFoundException e)
		{
			//TODO: notify prompt
		}
	}
}
